package org.pine.dictionary.service.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.extern.slf4j.Slf4j;
import org.pine.dictionary.common.StatusEnum;
import org.pine.dictionary.dao.entity.DictionaryDetail;
import org.pine.dictionary.dao.mapper.DictionaryDetailMapper;
import org.pine.dictionary.service.bo.DictionaryDetailBo;
import org.pine.dictionary.service.query.DictionaryDetailQuery;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

/**
 * <p>
 * 字典明细管理
 * </p>
 *
 * @author pine
 * @since 2024-05-17
 */
@Slf4j
@Service
public class DictionaryDetailManager extends BaseManager<DictionaryDetail, DictionaryDetailBo, DictionaryDetailMapper, DictionaryDetailQuery, Long, String> {
    /**
     * 查询单条数据
     *
     * @param businessId 业务ID
     * @return 业务对象
     */
    @Override
    public DictionaryDetailBo getOne(String businessId) {
        return getOne(new DictionaryDetailQuery().setDictionaryDetailId(businessId));
    }

    /**
     * 查询单条数据
     *
     * @param query 查询参数
     * @return 业务对象
     */
    @Override
    public DictionaryDetailBo getOne(DictionaryDetailQuery query) {
        // 处理业务查询条件
        LambdaQueryWrapper<DictionaryDetail> queryWrapper = this.encapsulateQueryWrapper(query);
        queryWrapper.last(" limit 1");

        // 查询单条数据
        DictionaryDetail dictionaryDetail = this.mapper.selectOne(queryWrapper);
        if (Objects.isNull(dictionaryDetail)) {
            return null;
        }

        // 根据查询结果转换业务对象
        return createBo(Collections.singletonList(dictionaryDetail)).get(0);
    }

    @Override
    public LambdaQueryWrapper<DictionaryDetail> encapsulateQueryWrapper(DictionaryDetailQuery request) {
        // 处理业务查询条件
        LambdaQueryWrapper<DictionaryDetail> queryWrapper = super.encapsulateQueryWrapper(request);
        queryWrapper.eq(StrUtil.isNotBlank(request.getDictionaryId()), DictionaryDetail::getDictionaryId, request.getDictionaryId());
        queryWrapper.eq(StrUtil.isNotBlank(request.getDictionaryDetailId()), DictionaryDetail::getDictionaryDetailId, request.getDictionaryDetailId());
        queryWrapper.eq(StrUtil.isNotBlank(request.getDictionaryValueCode()), DictionaryDetail::getDictionaryValueCode, request.getDictionaryValueCode());
        queryWrapper.like(StrUtil.isNotBlank(request.getDictionaryValueName()), DictionaryDetail::getDictionaryValueName, request.getDictionaryValueName());
        queryWrapper.eq(Objects.nonNull(request.getStatus()), DictionaryDetail::getStatus, request.getStatus());
        return queryWrapper;
    }

    /**
     * 通过业务id列表批量删除
     *
     * @param businessIds 业务id列表
     */
    @Override
    public void removeBatchByIds(Collection<String> businessIds) {
        super.removeBatchByIds(businessIds, DictionaryDetail::getDictionaryDetailId);
    }

    /**
     * 修改状态
     *
     * @param dictionaryIds 字典id列表
     * @param status        状态
     */
    public void changeStatusByDictionaryIds(Collection<String> dictionaryIds, StatusEnum status) {
        changeStatus(dictionaryIds, DictionaryDetail::getDictionaryId, status);
    }

    /**
     * 通过字典明细ID列表修改状态
     *
     * @param dictionaryDetailIds 字典明细id列表
     * @param status              状态
     */
    public void changeStatusByDictionaryDetailIds(Collection<String> dictionaryDetailIds, StatusEnum status) {
        changeStatus(dictionaryDetailIds, DictionaryDetail::getDictionaryDetailId, status);
    }


    public <BusinessId> void changeStatus(Collection<BusinessId> businessIds, SFunction<DictionaryDetail, BusinessId> column, StatusEnum status) {
        if (CollUtil.isNotEmpty(businessIds)) {
            LambdaUpdateWrapper<DictionaryDetail> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.in(column, businessIds);
            updateWrapper.ne(DictionaryDetail::getStatus, status);
            updateWrapper.set(DictionaryDetail::getStatus, status);
            updateWrapper.set(DictionaryDetail::getUpdateTime, LocalDateTime.now());
            // TODO 更新的用户信息，待维护
            updateWrapper.set(DictionaryDetail::getUpdateUserId, 0L);
            updateWrapper.set(DictionaryDetail::getUpdateUsername, "system");
            this.mapper.update(updateWrapper);
        }
    }
}
